package com.bishe.controller;

import cn.hutool.core.util.StrUtil;
import com.bishe.dao.UserDao;
import com.bishe.pojo.Business;
import com.bishe.pojo.Clientinfo;
import com.bishe.pojo.query.BusinessQuery;
import com.bishe.pojo.query.ClientQuery;
import com.bishe.pojo.query.PageResult;
import com.bishe.serivce.IBusinessService;
import com.bishe.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务控制层
 * @date 2022/03/02 36:11:09
 */
@RestController
@RequestMapping("/business")
@Api(tags = "客户业务")
@CrossOrigin
public class BusinessController {
    @Autowired
    private IBusinessService businessService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private UserDao userDao;

    @ApiOperation("获取当前登录人业务列表")
    @GetMapping("/toPage")
    public Result toPage(BusinessQuery query,HttpServletRequest request){

        JobUtil.checkLogin(request,userDao);

        SqlQueryUtils<Business> queryUtils = new SqlQueryUtils<>(Business.class,jdbcTemplate);
        // 查询条件不为空
        if (StrUtil.isNotEmpty(query.getType())){
            queryUtils.like(Business::getType,query.getType());
        }

        //创建人是自己
        queryUtils.eq(Business::getCreateBy,JobUtil.getUserId(request));
        // 分页
        List<Business> list = queryUtils.orderByDesc(Business::getCreateTime)
                .limit(query.getPageNo(), query.getPageSize()).getList();
        for (Business business : list) {
            Clientinfo one = new SqlQueryUtils<>(Clientinfo.class, jdbcTemplate).eq(Clientinfo::getId, business.getName()).getOne();
            if (one!=null){
                business.setName(one.getName());
            }
        }


        //返回数据处理
        PageResult<Business> pageResult = new PageResult<>(query, queryUtils, list);

        return Result.ok(pageResult);
    }

    @ApiOperation("获取所有业务列表")
    @GetMapping("/getAll")
    public Result getAll(BusinessQuery query,HttpServletRequest request){

        JobUtil.checkLogin(request,userDao);

        SqlQueryUtils<Business> queryUtils = new SqlQueryUtils<>(Business.class,jdbcTemplate);
        // 查询条件不为空
        if (StrUtil.isNotEmpty(query.getType())){
            queryUtils.like(Business::getType,query.getType());
        }

        // 分页
        List<Business> list = queryUtils.orderByDesc(Business::getCreateTime)
                .limit(query.getPageNo(), query.getPageSize()).getList();

        for (Business business : list) {
            Clientinfo one = new SqlQueryUtils<>(Clientinfo.class, jdbcTemplate).eq(Clientinfo::getId, business.getName()).getOne();
            if (one!=null){
                business.setName(one.getName());
            }
        }
        //返回数据处理
        PageResult<Business> pageResult = new PageResult<>(query, queryUtils, list);

        return Result.ok(pageResult);
    }


    @ApiOperation("添加或修改")
    @PostMapping("/addOrUpdate")
    public Result updateData(@RequestBody Business business, HttpServletRequest request){

        if (business == null){
            throw new EcoBootException("数据不能为空");
        }
        SqlQueryUtils<Business> queryUtils = new SqlQueryUtils<>(Business.class,jdbcTemplate);
        // 根据name查询单条记录
        Business one = queryUtils.eq(Business::getId, business.getId()).getOne();

        // 不存在 添加
        if (one == null){
            business.setId(StrUtil.uuid());
            business.setCreateTime(new Date());
            business.setCreateBy(JobUtil.getUserId(request));
            businessService.save(business);
            return Result.ok("添加成功");
        }else{
            //修改
            businessService.updateById(business);
            return Result.ok("修改成功");
        }
    }


    @ApiOperation("根据id删除")
    @DeleteMapping("delete/{id}")
    public Result deleteById(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }

        businessService.deleteById(id);
        return Result.ok("删除成功");
    }

}
